package com.myhome.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myhome.config.constant.R;
import com.myhome.domain.UserBank;
import com.myhome.dto.BankDto;
import com.myhome.service.UserBankService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @Author QYiXin
 * @Date 2021 10 13 18 18
 **/
@RestController
@RequestMapping("/userBanks")
public class BankController {
    @Autowired
    UserBankService userBankService;
    @GetMapping
    @ApiOperation(value = "分页查询用户的银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "usrId" ,value = "会员的Id") ,
            @ApiImplicitParam(name = "current" ,value = "当前页")  ,
            @ApiImplicitParam(name = "size" ,value = "每页显示的条数")
    })
    @PreAuthorize("hasAuthority('user_bank_query')")
    public R<Page<UserBank>> getUserBanks(@ApiIgnore Page<UserBank> page,Long userId){
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<UserBank> result = userBankService.getPagesBanks(page, userId);
        return R.ok(result);
    }

    @PostMapping("/status")
    @ApiOperation("修改状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" ,value = "银行卡的id") ,
            @ApiImplicitParam(name = "status" ,value = "银行卡的状态") ,
    })
    public R updateUserStatus(Long id,Byte status){
        UserBank bank = new UserBank();
        bank.setId(id);
        bank.setStatus(status);
        boolean isUpdate = userBankService.updateById(bank);
        if (isUpdate) {
            return R.ok();
        }
        return R.fail("修改失败");
    }

    @PatchMapping
    @ApiOperation(value = "修改银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userBank" ,value = "银行卡的json数据") ,
    })
    public R updateUser(@RequestBody UserBank userBank){
        boolean isUpdate = userBankService.updateById(userBank);
        if (isUpdate) {
            return R.ok();
        }
        return R.fail("修改失败");
    }

    @GetMapping("/current")
    @ApiOperation(value = "查询用户的卡号")
    public R<UserBank> getUserBank(){
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        UserBank bank = userBankService.getCurrentUserBank(userId);
        return R.ok(bank);
    }

    @PostMapping("/bind")
    @ApiOperation(value = "绑定银行卡")
    public R bindBank(@RequestBody UserBank userBank) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        Boolean isBind = userBankService.bindUserBank(userId,userBank);
        if (isBind) {
            return R.ok();
        }
        return R.fail("绑定失败");
    }
    @GetMapping("/getUserBankDto/{userId}")
    @ApiOperation("查询用户部分信息")
    public BankDto getUserBankInfo(@PathVariable("userId") Long userId){
        BankDto userBankDto = userBankService.getSomeUserBankInfo(userId);
        return userBankDto;
    }
}
